10 #define popcount(x) __builtin_popcount(x)
18 state(int I
, int V
, double W
, vector
<int> O
) : i(I
), v(V
), w(W
), o(O
) {}
19 bool operator < (const state
&that
) const {
27 while (cin
>> n
&& n
){
29 vector
<pair
<int, int> > p(n
);
30 for (int i
=0; i
<n
; ++i
){
31 cin
>> p
[i
].first
>> p
[i
].second
;
35 for (int i
=0; i
<n
; ++i
){
36 for (int j
=0; j
<n
; ++j
){
37 g
[i
][j
] = hypot(p
[i
].first
- p
[j
].first
, p
[i
].second
- p
[j
].second
) + 16.0;
42 for (int i
=0; i
<(1<<n
); ++i
){
43 for (int j
=0; j
<n
; ++j
){
48 priority_queue
<state
> q
;
49 for (int i
=0; i
<n
; ++i
){
51 q
.push(state(i
, 1<<i
, 0.0, vector
<int>(1, i
)));
58 //printf("Saqué de la pila: i = %d, v = %x, w = %lf\n", top.i, top.v, top.w);
60 if (d
[top
.v
][top
.i
] < top
.w
) continue;
62 if (popcount(top
.v
) == n
){ //Solution
63 printf("**********************************************************\n");
64 printf("Network #%d\n", N
++);
66 for (int i
=0; i
<n
-1; ++i
){
67 printf("Cable requirement to connect (%d,%d) to (%d,%d) is %.2f feet.\n",
68 p
[top
.o
[i
]].first
, p
[top
.o
[i
]].second
,
69 p
[top
.o
[i
+1]].first
, p
[top
.o
[i
+1]].second
,
70 g
[top
.o
[i
]][top
.o
[i
+1]]);
71 t
+= g
[top
.o
[i
]][top
.o
[i
+1]];
73 printf("Number of feet of cable required is %.2f.\n", t
);
77 for (int i
=0; i
<n
; ++i
){
78 //printf("Intetando ir al vecino %d\n", i);
79 if ((top
.v
& (1<<i
)) == 0){ //Si no había visitado el i
80 if (top
.w
+ g
[top
.i
][i
] < d
[top
.v
| (1<<i
)][i
]){
81 d
[top
.v
| (1<<i
)][i
] = top
.w
+ g
[top
.i
][i
];
83 q
.push(state(i
, top
.v
| (1<<i
), top
.w
+ g
[top
.i
][i
], top
.o
));
84 top
.o
.erase(top
.o
.end() - 1);